﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGauges.Win.Base;
using DevExpress.XtraGauges.Base;
using DevExpress.XtraGauges.Win;
using DevExpress.XtraGauges.Core.Model;
using CommonData;

namespace DataCentral.Forms
{
    public partial class TimeZoneTool : CommonChildForm
    {
        bool _IsLoading = false;
        public TimeZoneTool()
        {
            InitializeComponent();
        }

        private void DoAnimation()
        {
            string strUCT = lsbTimezone.SelectedValue.ToString();
            double dHours = 0;
            double dMinute = 0;
            string strTime = strUCT.Substring(strUCT.IndexOf('(')+1, strUCT.IndexOf(')') - strUCT.IndexOf('(')-1);
            if (strTime.IndexOf('+') > 0)
            {
                dHours = Convert.ToDouble(strTime.Substring(strTime.IndexOf('+')+1, strTime.IndexOf(':') - strTime.IndexOf('+')-1));
                dMinute = Convert.ToDouble(strTime.Substring(strTime.IndexOf(':')+1, strTime.Length - strTime.IndexOf(':') -1));
            }
            else if (strTime.IndexOf('-') > 0)
            {
                dHours = Convert.ToDouble(strTime.Substring(strTime.IndexOf('-')+1, strTime.IndexOf(':') - strTime.IndexOf('-')-1));
                dMinute = Convert.ToDouble(strTime.Substring(strTime.IndexOf(':')+1, strTime.Length - strTime.IndexOf(':') -1 ));
            }
            else
            {
                dHours = 0;
                dMinute = 0;
            }
            DateTime currentUTC = DateTime.UtcNow.AddHours(dHours);
            currentUTC = currentUTC.AddMinutes(dMinute);
            string strLocation = strUCT.Substring(strUCT.IndexOf(')')+1, strUCT.Length - strUCT.IndexOf(')')-1).Trim();

            UpdateClock(currentUTC, cGauge.Scales[0], cGauge.Scales[1], cGauge.Scales[2]);
            arcScale.Labels[0].Text = strLocation;
        }

        void UpdateClock(DateTime dt, IArcScale h, IArcScale m, IArcScale s)
        {
            int hour = dt.Hour <= 12 ? dt.Hour : dt.Hour - 12;
            int min = dt.Minute;
            int sec = dt.Second;
            h.Value = (float)hour + (float)(min) / 60.0f;
            m.Value = ((float)min + (float)(sec) / 60.0f) / 5f;
            s.Value = sec / 5.0f;
        }

        private void TimeZoneTool_Load(object sender, EventArgs e)
        {
            _IsLoading = true;
            for (int i =0; i <cbbTimezone.Properties.Items.Count;i++)
            {
                lsbTimezone.Items.Add(cbbTimezone.Properties.Items[i]);
            }
            lsbTimezone.SelectedIndex = 0;
            timer.Enabled = true;
            _IsLoading = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
             DoAnimation();
        }

        private void lsbTimezone_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_IsLoading)
            {
                timer.Enabled = false;
                DoAnimation();
                timer.Enabled = true;
            }
        }

        private void TimeZoneTool_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }
    }
}